package com.btprice.plugin;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.kotlin.KotlinFile;
import org.mybatis.generator.api.dom.kotlin.KotlinFunction;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.List;

public class MysqlPagers extends PluginAdapter {
    private Logger log = LoggerFactory.getLogger("mysql page plugin");
    public boolean validate(List<String> list) {
        if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false ){
            log.warn("pager:插件"+this.getClass().getTypeName()+"要求运行targetRuntime必须为MyBatis3！");
            return false;
        }
        return true;
    }

    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        final PrimitiveTypeWrapper primitiveTypeWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();

        Field limit = new Field("limit",primitiveTypeWrapper);
        limit.setVisibility(JavaVisibility.PRIVATE);
        topLevelClass.addField(limit);

        Method setLimit = new Method("setLimit");
        setLimit.setVisibility(JavaVisibility.PUBLIC);
        setLimit.addParameter(new Parameter(primitiveTypeWrapper,"limit"));
        setLimit.addBodyLine("this.limit = limit;");
        topLevelClass.addMethod(setLimit);

        Method getLimit = new Method("getLimit");
        getLimit.setVisibility(JavaVisibility.PUBLIC);
        getLimit.setReturnType(primitiveTypeWrapper);
        getLimit.addBodyLine("return limit;");
        topLevelClass.addMethod(getLimit);


        Field offset = new Field("offset",primitiveTypeWrapper);
        limit.setVisibility(JavaVisibility.PRIVATE);
        topLevelClass.addField(offset);

        Method setOffset = new Method("setOffset");
        setOffset.setVisibility(JavaVisibility.PUBLIC);
        setOffset.addParameter(new Parameter(primitiveTypeWrapper,"offset"));
        setOffset.addBodyLine("this.offset = offset;");
        topLevelClass.addMethod(setOffset);

        Method getOffset = new Method("getOffset");
        getOffset.setVisibility(JavaVisibility.PUBLIC);
        getOffset.setReturnType(primitiveTypeWrapper);
        getOffset.addBodyLine("return offset;");
        topLevelClass.addMethod(getOffset);

        return true;
    }

    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        final XmlElement anIfLimit = new XmlElement("if");
        anIfLimit.addAttribute(new Attribute("test","limit != null"));

        final XmlElement anIfOffer = new XmlElement("if");
        anIfOffer.addAttribute(new Attribute("test","offset != null"));
        anIfOffer.addElement(new TextElement("limit #{offset},#{limit}"));
        anIfLimit.addElement(anIfOffer);

        final XmlElement anIfOffsetNull = new XmlElement("if");
        anIfOffsetNull.addAttribute(new Attribute("test","offset == null"));
        anIfOffsetNull.addElement(new TextElement("limit #{limit}"));
        anIfLimit.addElement(anIfOffsetNull);

        element.addElement(anIfLimit);


        return true;
    }

    @Override
    public boolean clientBasicSelectOneMethodGenerated(KotlinFunction kotlinFunction, KotlinFile kotlinFile, IntrospectedTable introspectedTable) {
        return false;
    }
}
